package demoMod.nanaki.vfx;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.MathUtils;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.vfx.AbstractGameEffect;
import demoMod.nanaki.NanakiMod;

public class StropheParticleEffect extends AbstractGameEffect {
    private Texture img;
    private float x;
    private float y;
    private float vX;
    private float vY;
    private float targetScale;
    private float vR;

    public StropheParticleEffect(float startX, float startY, ParticleType type) {
        this.img = type.getImg();
        this.x = startX + MathUtils.random(-100.0F * Settings.scale, 100.0F * Settings.scale);
        this.y = startY;
        this.vY = MathUtils.random(100.0F, 150.0F) * Settings.scale;
        this.vX = MathUtils.random(-100.0F, 100.0F) * Settings.scale;
        this.scale = 0.0F;
        this.targetScale = MathUtils.random(0.3F, 0.75F) * Settings.scale;
        this.startingDuration = this.duration = 2.0F;
        this.color = Color.WHITE.cpy();
        this.color.a = 0.0F;
        this.vR = MathUtils.random(-30.0F, 30.0F);
    }

    @Override
    public void update() {
        this.y += this.vY * Gdx.graphics.getDeltaTime();
        this.x += this.vX * Gdx.graphics.getDeltaTime();

        float elapsed = this.startingDuration - this.duration;
        if (elapsed < 0.5F) {
            this.color.a = Interpolation.pow2Out.apply(elapsed * 2.0F);
            this.scale = Interpolation.pow2Out.apply(0.0F, this.targetScale, elapsed * 2.0F);
        } else if (elapsed >= 1.5F) {
            this.color.a = 1.0F - Interpolation.pow2Out.apply(1.0F - (2.0F - elapsed) * 2.0F);
        }
        this.rotation += this.vR * Gdx.graphics.getDeltaTime();

        this.duration -= Gdx.graphics.getDeltaTime();
        if (duration < 0.0F) {
            isDone = true;
            dispose();
        }
    }

    @Override
    public void render(SpriteBatch sb) {
        sb.setColor(this.color);
        sb.draw(img, this.x, this.y, 64.0F, 64.0F, 128.0F, 128.0F, this.scale, this.scale, this.rotation, 0, 0, 128, 128, false, false);
    }

    @Override
    public void dispose() {
        if (img != null) {
            img.dispose();
            img = null;
        }
    }

    public enum ParticleType {
        FOVER,
        NOTE
        ;
        public Texture getImg() {
            switch (this) {
                case FOVER:
                default:
                    String size = MathUtils.randomBoolean() ? "l" : "s";
                    int index = MathUtils.random(4) + 1;
                    return new Texture(NanakiMod.getResourcePath("stances/clover_" + size + index + ".png"));
                case NOTE:
                    index = MathUtils.random(2) + 1;
                    return new Texture(NanakiMod.getResourcePath("stances/note_" + index + ".png"));
            }
        }
    }
}
